<?php

namespace app\member\controller;

use think\Controller;

class Index extends Common {

    public function index() {
        $plist = model('Supply')->where('mid',$this->memberInfo->id)->order('id desc')->paginate(10);
        $tlist = model('Types')->column('id,title');
        $mlist = model('Member')->column('id,company');
        $this->assign('plist', $plist);
        $this->assign('page', $plist->render());
        $this->assign('tlist', $tlist);
        $this->assign('mlist', $mlist);
        return $this->fetch();
    }

    public function baseinfo() {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['id'] = $this->memberInfo->id;
            // 验证数据
            $result = $this->validate($data, 'Member.updateSelf');
            if (true !== $result) {
                $this->error($result);
            }
            $Member = model('Member');
            $data['password'] = $Member->setPassword($data['password']);
            try {
                $Member->allowField(['company', 'address', 'nickname', 'telephone', 'email'])->save($data, ['id' => $data['id']]);
            } catch (\Exception $ex) {
                $this->error($ex->getMessage());
            }
            $this->success('信息修改成功~');
        } else {
            $this->assign('info', $this->memberInfo);
            return $this->fetch();
        }
    }

    public function setting() {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['id'] = $this->memberInfo->id;
            // 验证数据
            $result = $this->validate($data, 'Member.passwordself');
            if (true !== $result) {
                $this->error($result);
            }
            $Member = model('Member');
            $data['password'] = $Member->setPassword($data['password']);
            try {
                $Member->allowField(['password'])->save($data, ['id' => $data['id']]);
            } catch (\Exception $ex) {
                $this->error($ex->getMessage());
            }
            $this->success('密码修改成功~');
        } else {
            return $this->fetch();
        }
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // dump($data);
            // exit();
            //$data['modelField']['typeid']=$data['typeid'];
            $data['modelField']['status']=0;
            $data['modelField']['mid']=$this->memberInfo->id;
            $Supply=model('Supply');
            try {
                $Supply->allowField(['title', 'pictures', 'supplyinfo', 'phone', 'email', 'contacts', 'address', 'class', 'status','typeid', 'mid'])->save($data['modelField']);
            } catch (\Exception $ex) {
                $this->error($ex->getMessage());
            }
            $this->success('产品添加成功~', url('index'));

        } else {
            $tlist = model('Types')->getTypes('id,path,title,orders');
            // dump($tlist);
            // exit();
            $this->assign('tlist', $tlist);
            // $glist = model('MemberGroup')->column('id,title');
            // $this->assign('glist', $glist);
            //产品列表
            return $this->fetch();
        }
    }

    public function delete($id = 0) {
        if ($this->request->isPost()) {
            $ids = input('post.ids/a', null, 'intval');
            if (empty($ids)) {
                $this->error('请先勾选需要删除的产品~');
            }
            if (model('Supply')->destroy($ids)) {
                $this->success('产品删除成功~');
            } else {
                $this->error('产品删除失败~');
            }
        } else {
            $id = intval($id);
            if (model('Supply')->destroy($id)) {
                return true;
            } else {
                return '数据库操作失败';
            }
        }
    }

    public function edit($id = 0) {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['id'] = intval($data['id']);
            // $Supply=model('Supply')->where('id', $id)->find();
            try {
                model('Supply')->where('id', $id)->update(['class'=>$data['modelField']['class'], 'typeid'=>$data['modelField']['typeid'], 'title'=>$data['modelField']['title'], 'pictures'=>$data['modelField']['pictures'], 'supplyinfo'=>$data['modelField']['supplyinfo']]);
            } catch (\Exception $ex) {
                $this->error($ex->getMessage());
            }
            $this->success('产品编辑成功~', url('index'));
        } else {
            $id = intval($id);
            if ($id < 1) {
                $this->error('参数错误~');
            }
            $plist = model('Supply')->where('id', $id)->find();
            $tlist = model('Types')->getTypes('id,path,title,orders');
            $this->assign('plist', $plist);
            $this->assign('tlist', $tlist);
            return $this->fetch();
        }
    }

    public function changeState($id, $status) {
        $id = intval($id);
        $status = intval($status);
        if ($id <= 0 || ($status != 0 && $status != 1))
            return '参数错误';
        if (model('Supply')->where('id', $id)->update(['status' => $status])) {
            return true;
        } else {
            return '设置失败';
        }
    }

    public function delp($id = 0) {
        if ($this->request->isPost()) {
            $ids = input('post.ids/a', null, 'intval');
            if (empty($ids)) {
                $this->error('请先勾选需要删除的产品~');
            }
            if (model('Supply')->destroy($ids)) {
                $this->success('产品删除成功~');
            } else {
                $this->error('产品删除失败~');
            }
        } else {
            $id = intval($id);
            if (model('Supply')->destroy($id)) {
                return true;
            } else {
                return '数据库操作失败';
            }
        }
    }
}
